﻿
Public Class DataGridViewHelper

    Private Sub New() ' zorg dat er geen publieke constructor is
    End Sub

    Public Shared Function GetGridColumnByDataName(ByVal kolomNaam As String, ByVal grid As DataGridView) As DataGridViewColumn
        'Return grid.Columns(kolomNaam)
        For Each kolom As DataGridViewColumn In grid.Columns
            If (kolom.DataPropertyName.ToUpper() = kolomNaam.ToUpper()) Then Return kolom
        Next
        Return Nothing
    End Function

    Public Shared Function GetValueFromDataGridRow(ByVal veldNaam As String, ByVal row As DataGridViewRow) As Object
        Return row.Cells(GetGridColumnByDataName(veldNaam, row.DataGridView).Index).Value
    End Function

    Public Shared Function GetValueFromDataGridRow(Of T)(ByVal veldNaam As String, ByVal row As DataGridViewRow) As T
        Return DirectCast(row.Cells(GetGridColumnByDataName(veldNaam, row.DataGridView).Index).Value, T)
    End Function

    Public Shared Function GetDataRowFromDataGridViewRow(Of T As DataRow)(ByVal row As DataGridViewRow) As T
        If (row.DataBoundItem Is Nothing) Then Return Nothing
        Dim drv As DataRowView = DirectCast(row.DataBoundItem, DataRowView)
        Return DirectCast(drv.Row, T)
    End Function

End Class
